package com.example.controller;

import com.example.pojo.DTO.FlightsDTO;
import com.example.pojo.FlightBean;
import com.example.pojo.Flights;
import com.example.pojo.MyPageData;
import com.example.pojo.Result;
import com.example.pojo.VO.AirlinesVO;
import com.example.service.FlightService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/flight")
public class FlightController {
    @Autowired
    private FlightService flightService;
    //查询所有航班的信息+分页
    @PostMapping("/{pageNum}/{pageSize}")
    public Result<MyPageData> getAllFightsInfo(@PathVariable("pageNum") Integer pageNum,
                                               @PathVariable("pageSize") Integer pageSize,
                                               @RequestBody(required = false) FlightsDTO flightsDTO
                                       ){
        PageHelper.startPage(pageNum,pageSize);
        List<Flights> flights=flightService.getFightsByCondition(flightsDTO);
        PageInfo<Flights> info = new PageInfo<>(flights);
        MyPageData myPageData = new MyPageData(info.getTotal(),info.getList());
        return new Result(200,"查询成功！",myPageData);


    }
    //根据Id删除航班信息
    @DeleteMapping("/delete/{id}")
    public Result deleteFlightById(@PathVariable("id") Integer id){
     Integer num =flightService.deleteFlightById(id);
        return new Result(200,"删除成功！",num);
    }
    //根据id查询航班信息
    @GetMapping("/{id}")
    public Result<Flights> getFlightById(@PathVariable("id") Integer id){
        Flights flights = flightService.getFlightById(id);
        return new Result(200,"查询成功！",flights);
    }
    //根据id修改航班信息
    @PutMapping("/update")
    public Result updateFlightById(@RequestBody FlightBean flightBean){
        Integer num = flightService.updateFlightById(flightBean);
        return new Result(200,"修改成功！",num);
    }
    //新增航班信息
    @PostMapping("/add")
    public Result addFlight(@RequestBody FlightBean flightBean){
        Integer num = flightService.addFlight(flightBean);
        return new Result(200,"新增成功！",num);
    }
    @GetMapping("/getAirlinesName")
    public Result<AirlinesVO> getAirlinesName(){
        List<AirlinesVO> vos = flightService.getAirlinesName();
        return new Result(200,"查询成功！",vos);
    }


}
